package GUI;

import Exceptions.IncorrectInputException;
import classInterfaces.IGUI;
import gameEngine.Field;
import gameEngine.Player;

public class GUI implements IGUI {
	private Field theDots; // the entire field
	private char[][] guiField;
	
	/**
	 * constructor creates new instance of class GUI with given existaint field
	 * 
	 * @param f - class Field to create field of dots
	 */
	
	public void setNewField(Field f) throws IncorrectInputException {
		this.theDots.copy(f);
		makeChangesToGUI();
	}
	
	private void makeChangesToGUI() {
		for(int i = 0; i < this.guiField.length; i++)
			for(int j = 0; j < this.guiField[i].length; j++)
				if(i % 2 == 0 && j % 2 == 1 && this.guiField[i][j] == ' ' && this.theDots.field[i][j - 1].hasMovedRight())
					this.guiField[i][j] = '-';
				else if(i > 1 && i % 2 == 0 && j % 2 == 0 && this.guiField[i - 1][j] == ' ' && this.theDots.field[i- 1][j].hasMovedUp())
					this.guiField[i - 1][j] = '|';
	}

	public GUI(Field f) {
		Player[] p = new Player[2];
		
		this.theDots = Field.createField(0, 0, p);
		setNewField(f);
		
		this.guiField = new char[2*this.theDots.getRowLength() - 1][2*this.theDots.getColumnLength() - 1];
		setGuiField();
	}

	private void setGuiField() {
		for(int i = 0; i < this.guiField.length; i++)
			for(int j = 0; j < this.guiField[i].length; j++) 
				if(i % 2 == 0 && j % 2 == 0) 
					this.guiField[i][j] = '.';
				else 
					this.guiField[i][j] = ' ';
	}

	@Override
	public void drawRound() {
		for(int i = 0; i < guiField.length; i++) {
			for(int j = 0; j < guiField[i].length; j++) {
				System.out.print(guiField[i][j]);
			}
			System.out.println();
		}
	}

	@Override
	public void drawWinner() {
		String nameOfWinner = theDots.announceWinner().playerName();
		
		System.out.println("The winner of this game is " + nameOfWinner);
	}
	
	

}
